﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project.MODEL;
using Project.BLL;
using System.Dynamic;
using Newtonsoft.Json;

namespace Project.UI.Controllers
{
    public class ManageController : Controller
    {
        ManageBll bll = new ManageBll();

        // GET: Manage
        [CheckLogin]
        public ActionResult Index()
        {
            return View();
        }

        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }

        [AllowAnonymous]
        [HttpPost]
        public ActionResult Login(string userName, string pwd)
        {
            dynamic d = new ExpandoObject();
            if (userName==null || pwd==null)
            {
                d.code = -1;
                d.msg = "账户或密码不能为空！";
                return Content(JsonConvert.SerializeObject(d), "Application/Json");

            }
            var emp = bll.GetEmployeeByLogin(userName, pwd);
            if (emp!=null)
            {
                if (emp.status)
                {
                    Session["uid"] = emp.emID;
                    Session["name"] = emp.emName;
                    Session["rid"] = emp.roleID;
                    Session["headimg"] = emp.headImg;
                    Session["user"] = emp;
                    Session["auths"] = emp.Roles.Authority.ToList();

                    d.code = 1;
                    d.msg = "登录成功";
                }
                else
                {
                    d.code = -1;
                    d.msg = "该账户已经被冻结！";
                }
                
            }
            else
            {
                d.code = 0;
                d.msg = "账户或密码错误！";
            }

            return Content(JsonConvert.SerializeObject(d),"Application/Json");


        }

        [CheckLogin]
        public ActionResult GetMenus()
        {
            var menus = bll.GetMenusByRid(int.Parse(Session["rid"].ToString()));
            return Content(menus, "Application/Json");
        }

        [CheckLogin]
        public ActionResult SignOut() {
            Session.Clear();
            dynamic d = new ExpandoObject();
            d.code = 1;
            d.msg = "退出成功";
            return Content(JsonConvert.SerializeObject(d), "Application/Json");
        }
    }
}